package org.devocean.groupbuying.web.frontend;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.devocean.groupbuying.domain.Customer;
import org.devocean.groupbuying.domain.Deal;
import org.devocean.groupbuying.service.CustomerService;
import org.devocean.groupbuying.service.DealService;
import org.devocean.groupbuying.service.MenuService;
import org.devocean.groupbuying.service.ProductService;
import org.devocean.groupbuying.utils.UriUtils;
import org.devocean.groupbuying.web.frontend.displaybeans.CartBean;
import org.devocean.groupbuying.web.frontend.displaybeans.DealBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = { "/{lang:[a-z]{2}}/deals" })
// @RequestMapping(value={"", "/{lang:[a-z]{2}}"})
public class DealController {

	@Autowired
	private DealService dealService;

	@Autowired
	private CustomerService customerService;

	@Autowired
	private ProductService productService;

	@RequestMapping("")
	public String index(Map<String, Object> map,
			@CookieValue("clientLanguage") String lang) {

		return "redirect:/" + lang + "/deals/1/3";
	}

	@RequestMapping("{prod}/{index}/{max}")
	public String paginateByProd(Map<String, Object> map,
			@CookieValue("clientLanguage") String lang,
			@PathVariable("index") Integer index,
			@PathVariable("max") Integer max,
			@PathVariable("prod") Integer productID) {
		long count = dealService.countDeals(lang, productID);
		List<DealBean> dealBeans = new ArrayList<DealBean>();

		map.put("count", count);
		long pages = Math.round((new Float(count) / new Float(max)));
		if (pages < (new Float(count) / new Float(max))) {
			pages = pages + 1;
		}

		int start = (int) ((index * max) - max);
		for (Deal d : dealService
				.getDealsbyProduct(lang, productID, start, max)) {
			dealBeans.add(new DealBean(d));

		}
		final DealBean deal = new DealBean(dealService.getFeaturedDeal(lang));

		map.put("dealBean", deal);
		map.put("dealBeans", dealBeans);
		map.put("pages", pages);
		map.put("index", index);
		map.put("productTitle", productService.getProductById(productID)
				.getArticlesLocalization().getArticles().iterator().next()
				.getTitle());
		map.put("path", "deals/" + productID + "/");
		map.put("active", "all");
		return "dealsByProd";
	}

	@RequestMapping("{index}/{max}")
	public String paginate(Map<String, Object> map,
			@CookieValue("clientLanguage") String lang,
			@PathVariable("index") Integer index,
			@PathVariable("max") Integer max) {
		long count = dealService.countDeals(lang);
		List<DealBean> dealBeans = new ArrayList<DealBean>();

		map.put("count", count);
		long pages = Math.round((new Float(count) / new Float(max)));
		if (pages < (new Float(count) / new Float(max))) {
			pages = pages + 1;
		}

		int start = (int) ((index * max) - max);
		for (Deal d : dealService.getDealsEntries(lang, start, max)) {
			dealBeans.add(new DealBean(d));

		}
		final DealBean deal = new DealBean(dealService.getFeaturedDeal(lang));

		map.put("dealBean", deal);
		map.put("dealBeans", dealBeans);
		map.put("pages", pages);
		map.put("index", index);
		map.put("path", "deals/");
		map.put("active", "all");
		return "deals";
	}

	@RequestMapping("endsoon/{index}/{max}")
	public String paginateEndSoon(Map<String, Object> map,
			@CookieValue("clientLanguage") String lang,
			@PathVariable("index") Integer index,
			@PathVariable("max") Integer max) {
		long count = dealService.countDeals(lang);
		List<DealBean> dealBeans = new ArrayList<DealBean>();

		map.put("count", count);
		long pages = Math.round((new Float(count) / new Float(max)));
		if (pages < (new Float(count) / new Float(max))) {
			pages = pages + 1;
		}

		int start = (int) ((index * max) - max);
		for (Deal d : dealService.getEndSoonDealsEntries(lang, start, max)) {
			dealBeans.add(new DealBean(d));

		}
		final DealBean deal = new DealBean(dealService.getFeaturedDeal(lang));

		map.put("dealBean", deal);
		map.put("dealBeans", dealBeans);
		map.put("pages", pages);
		map.put("index", index);
		map.put("path", "deals/endsoon/");
		map.put("active", "ends");
		return "deals";
	}

	@RequestMapping("new/{index}/{max}")
	public String paginateNew(Map<String, Object> map,
			@CookieValue("clientLanguage") String lang,
			@PathVariable("index") Integer index,
			@PathVariable("max") Integer max) {
		long count = dealService.countDeals(lang);
		List<DealBean> dealBeans = new ArrayList<DealBean>();

		map.put("count", count);
		long pages = Math.round((new Float(count) / new Float(max)));
		if (pages < (new Float(count) / new Float(max))) {
			pages = pages + 1;
		}

		int start = (int) ((index * max) - max);
		for (Deal d : dealService.getNewDealsEntries(lang, start, max)) {
			dealBeans.add(new DealBean(d));

		}
		final DealBean deal = new DealBean(dealService.getFeaturedDeal(lang));

		map.put("dealBean", deal);
		map.put("dealBeans", dealBeans);
		map.put("pages", pages);
		map.put("index", index);
		map.put("path", "deals/new/");
		map.put("active", "new");
		return "deals";
	}

	@RequestMapping("popular/{index}/{max}")
	public String paginatePopular(Map<String, Object> map,
			@CookieValue("clientLanguage") String lang,
			@PathVariable("index") Integer index,
			@PathVariable("max") Integer max) {
		long count = dealService.countDeals(lang);
		List<DealBean> dealBeans = new ArrayList<DealBean>();

		map.put("count", count);
		long pages = Math.round((new Float(count) / new Float(max)));
		if (pages < (new Float(count) / new Float(max))) {
			pages = pages + 1;
		}

		int start = (int) ((index * max) - max);
		for (Deal d : dealService.getPopularDealsEntries(lang, start, max)) {
			dealBeans.add(new DealBean(d));

		}
		final DealBean deal = new DealBean(dealService.getFeaturedDeal(lang));

		map.put("dealBean", deal);
		map.put("dealBeans", dealBeans);
		map.put("pages", pages);
		map.put("index", index);
		map.put("path", "deals/popular/");
		map.put("active", "popular");
		return "deals";
	}

	@RequestMapping("/tags/{tag}")
	public String dealsByTag(ModelMap map,
			@CookieValue("clientLanguage") String lang,
			@PathVariable("tag") String tag) {
		List<DealBean> dealBeans = new ArrayList<DealBean>();
		for (Deal d : dealService.getDealsbyTag(lang, tag)) {
			dealBeans.add(new DealBean(d));
		}
		map.put("dealBeans", dealBeans);
		return "deals";
	}

	@RequestMapping(value = "{permalink:[0-9a-z_]+}-{dealID:\\d+}/purchases")
	public String buy(@PathVariable("dealID") Integer dealID,
			ModelMap modelMap, @CookieValue("clientLanguage") String lang) {
		final CartBean cartBean = new CartBean(dealService.getDealWithKey(
				dealID, lang));
		modelMap.addAttribute("cart", cartBean);
		return "customer/cart";
	}

	@RequestMapping("featured_deal")
	public String featuredDeal(@CookieValue("clientLanguage") String lang,
			ModelMap map) {
		final DealBean deal = new DealBean(dealService.getFeaturedDeal(lang));

		map.addAttribute("dealBean", deal);

		return "deal";
	}

	@RequestMapping(value = "{permalink:[0-9a-z_]+}-{dealID:\\d+}")
	public String dealDetail(@CookieValue("clientLanguage") String lang,
			@PathVariable("dealID") Integer dealID, ModelMap map,
			HttpServletRequest request) {

		final DealBean deal = new DealBean(
				dealService.getDealById(dealID, lang));
		map.addAttribute("dealBean", deal);
		return "deal";
	}

	@RequestMapping(value = "/{dealID}/share/{uuid}")
	public String dealDetailShared(@CookieValue("clientLanguage") String lang,
			@PathVariable("dealID") Integer dealID,
			@PathVariable("uuid") String customerUuid, ModelMap map,
			HttpServletRequest request) {

		Customer customer = customerService.getCustomerByUuid(customerUuid);

		if (customer != null) {
			HttpSession session = request.getSession();
			session.setAttribute("customerUuid", customer.getUuid());
		}
		Deal deal = dealService.getDealById(dealID);

		String uri = lang + "/deals/" + UriUtils.dealUri(deal);

		return "redirect:/" + uri;
	}

}
